package com.cn.sys.main.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.cn.sys.frame.dao.BaseSpringJdbcDao;
import com.cn.sys.frame.exception.ServiceException;
import com.cn.sys.frame.service.impl.BaseServiceImpl;
import com.cn.sys.frame.util.page.PageData;
import com.cn.sys.frame.util.query.QueryBean;
import com.cn.sys.frame.util.query.QueryCondition;
import com.cn.sys.main.annotation.OperationDescription;
import com.cn.sys.main.dao.SysDictionaryDao;
import com.cn.sys.main.entity.SysDictionary;
import com.cn.sys.main.service.SysDictionaryService;

@Service("sysDictionaryService")
public class SysDictionaryServiceImpl extends BaseServiceImpl<SysDictionary, Long> implements
		SysDictionaryService {

	private SysDictionaryDao sysDictionaryDao;
	
	@Autowired
	public void setBaseDao(BaseSpringJdbcDao<SysDictionary, Long> sysDictionaryDao) {
		this.baseDao = sysDictionaryDao;
		this.sysDictionaryDao = (SysDictionaryDao) sysDictionaryDao;
	}
	@Cacheable(value="sysDictCache", key=" #id + 'get'")
	public SysDictionary get(Long id) throws Exception {
		return super.get(id);
	}

	@OperationDescription(operateType = "save", 
			content = "添加数据字典\nID:${#entity.id}\n代码:${#entity.code}\n父代码:${#entity.parentCode}\n名称:${#entity.name}\n值:${#entity.val}", 
			operateObj = "SysDictionary")
	public SysDictionary save(SysDictionary entity) throws Exception {
		return super.save(entity);
	}
	
	public List<SysDictionary> getAll() throws Exception {
		return super.getAll();
	}

	public long getCount(QueryCondition queryCondition) throws Exception {
		return super.getCount(queryCondition);
	}

	public PageData<SysDictionary> getPageData(PageData<SysDictionary> pageData, QueryCondition queryCondition)
			throws Exception {
		return super.getPageData(pageData, queryCondition);
	}

	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class, readOnly=false)
	@OperationDescription(operateType = "udpate", 
			content = "修改数据字典\nID:${#entity.id}\n代码:${#entity.code}\n父代码:${#entity.parentCode}\n名称:${#entity.name}\n值:${#entity.val}", 
			operateObj = "SysDictionary")
	@CacheEvict(value="sysDictCache", allEntries=true)
	public SysDictionary update(SysDictionary entity) throws Exception {
		try {
			SysDictionary dict = sysDictionaryDao.get(entity.getId());
			List<SysDictionary> dictList = this.getDictByParentCode(dict.getCode());
			if(dictList!=null&&!dictList.isEmpty()){
				for (SysDictionary sysDict : dictList) {
					sysDict.setParentCode(entity.getCode());
				}
				sysDictionaryDao.batchUpdate(dictList);
			}
			sysDictionaryDao.update(entity);
		} catch (Exception e) {
			//e.printStackTrace();
			throw new ServiceException("修改失败!["+e.getMessage()+"]");
		}
		return entity;
	}

	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class, readOnly=false)
	@OperationDescription(operateType = "delete", content = "删除数据字典\nID:${#id}", operateObj = "SysDictionary")
	@CacheEvict(value="sysDictCache", allEntries=true)
	public int delete(Long id) throws Exception {
		QueryCondition queryCondition = new QueryCondition();
		int result = 0;
		try {
			SysDictionary dict = this.get(id);
			if(dict!=null){
				queryCondition.setQueryBean(new QueryBean(" WHERE PARENT_CODE = ?", "PARENT_CODE", dict.getCode()));
				sysDictionaryDao.delete(queryCondition);
				result = sysDictionaryDao.delete(id);
			}else {
				result ++;
			}
		} catch (Exception e) {
			//e.printStackTrace();
			throw new ServiceException("删除失败!["+e.getMessage()+"]");
		}
		return result;
	}
	
	public List<SysDictionary> getDictByParentCode(String parentCode)
			throws Exception {
		QueryCondition queryCondition = new QueryCondition();
		queryCondition.setQueryBean(new QueryBean(" AND PARENT_CODE = ?", "PARENT_CODE", parentCode));
		List<SysDictionary> dictList = sysDictionaryDao.listByCondition(queryCondition);
		return dictList;
	}
}
